package edu.tyut.wc;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

import java.nio.charset.StandardCharsets;

public class WordCountBatchDemo {
    public static void main(String[] args) throws Exception {
        // 1. 创建执行环境
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        // 2. 读取数据
        DataSource<String> lineDS = env.readTextFile("src/main/resources/words.txt", StandardCharsets.UTF_8.name());
        lineDS.flatMap((String line, Collector<Tuple2<String, Integer>> out) -> {
                    String[] strings = line.split("\\W+");
                    for (String string : strings) {
                        out.collect(new Tuple2<>(string, 1));
                    }
                })
                .returns(Types.TUPLE(Types.STRING, Types.INT))
                .groupBy(0)
                .sum(1)
                .print();
        // 3. 处理数据
        // 4. 打印
    }
}
